package com.proxy.dynamic_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Tank implements Movable {

    @Override
    public void move() throws InterruptedException {
        System.out.println("tank move...");
        Thread.sleep(5);
    }

    public static void main(String[] args) throws InterruptedException {
        Tank tank = new Tank();
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        Movable m = (Movable) Proxy.newProxyInstance(Tank.class.getClassLoader(),
                new Class[]{Movable.class},
                new LogHander(tank)
        );
        m.move();
    }
}

class LogHander implements InvocationHandler{
    Tank tank;

    public LogHander(Tank tank) {
        this.tank = tank;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("method " + method.getName() + " start...");
        Object o = method.invoke(tank, args);
        System.out.println("method " + method.getName() + " end...");
        return o;
    }
}

interface Movable{
    void move() throws InterruptedException;
}